package com.adou.Controller;

import com.adou.common.controller.BaseController;
import com.adou.common.dto.SearchConditionDto;
import com.adou.common.dto.SearchConditionOutDto;
import com.adou.common.model.SearchCondition;
import com.adou.common.util.Const;
import com.adou.service.FormSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 表单及查询处理控制器
 */
@Controller
@Scope(value = "prototype")
@RequestMapping(value = "/formsearch/")
public class FormSearchController extends BaseController {

    @Autowired
    private FormSearchService service;


    /**
     * 查询条件列表分页
     *
     * @param pageNum  页数
     * @param pageSize 页大小
     * @param keyWord  模糊查询：条件类型、字段名称1、字段名称2
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findByPage(int pageNum, int pageSize, String keyWord) {
        try {
            SearchCondition searchCondition = new SearchCondition();
            searchCondition.setKeyWord(keyWord);
            Map<String, Object> map = service.findByPage(pageNum, pageSize, searchCondition);
            if ((long) map.get("totalcount") > 0) {
                List<SearchCondition> searchConditionList=(List<SearchCondition>)map.get("list");
                List<SearchConditionOutDto> searchConditionOutDtoList = searchConditionList.stream().map(SearchConditionOutDto::new).collect(Collectors.toList());
                map.put("list",searchConditionOutDtoList);
                resultMap.put("data", map);
                resultMap.put("msg", Const.DATA_SUCCEED);
            } else {
                resultMap.put("result", Const.LIST_ERROR_101);
                resultMap.put("msg", Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.LIST_ERROR_102);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

    /**
     * 查询条件明细
     *
     * @param id 条件id
     */
    @RequestMapping(value = "find", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> find(Long id) {
        try {
            SearchCondition searchCondition = new SearchCondition();
            searchCondition.setId(id);
            Map<String, Object> map= service.findSearchCondition(searchCondition);
            if ((int)map.get("res")==1) {
                resultMap.put("data", map.get("data"));
                resultMap.put("msg", Const.DATA_SUCCEED);
            } else {
                resultMap.put("result", Const.DATA_ERROR_111);
                resultMap.put("msg", Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DATA_ERROR_112);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

    /**
     * 保存查询条件
     *
     * @param o type 条件类型 ;findname1 条件显示名称;findfield1 条件比较字段;condition 条件比较符;findname2 条件显示名称;findfield2 条件比较字段
     * @param fids 条件关联表单id
     *
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertSearchCondition(SearchConditionDto o, String fids) {
        try {
            SearchCondition searchCondition=new SearchCondition(o);
            int res = service.insertSearchCondition(searchCondition,fids);
            if (res == 1) {
                resultMap.put("msg", Const.SAVE_SUCCEED);
            }else if(res==211){
                error(res,"搜索类型为空");
            }else if(res==212){
                error(res,"字段findfield1为空");
            }else if(res==213){
                error(res,"条件比较符为空");
            }else if(res==214){
                error(res,"当前字段查询已存在");
            }else if(res==215){
                error(res,"表单id为空");
            }else if(res==216){
                error(res,"表单id格式错误");
            }else {
                resultMap.put("result", Const.ADD_ERROR_201);
                resultMap.put("msg", Const.SAVE_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.ADD_ERROR_202);
            resultMap.put("msg", Const.SAVE_FAIL);
        }
        return resultMap;
    }

    /**
     * 更新查询条件
     *
     * @param o id 条件id;type 条件类型 ;findname1 条件显示名称;findfield1 条件比较字段;condition 条件比较符;findname2 条件显示名称;findfield2 条件比较字段
     * @param fids 条件关联表单id
     *
     * ****
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateSearchCondition(SearchConditionDto o,String fids) {
        try {
            SearchCondition searchCondition=new SearchCondition(o);
            int res = service.updateSearchCondition(searchCondition,fids);
            if (res == 1) {
                resultMap.put("msg", Const.UPDATE_SUCCEED);
            }else if(res==211){
                error(res,"搜索类型为空");
            }else if(res==212){
                error(res,"字段findfield1为空");
            }else if(res==213){
                error(res,"条件比较符为空");
            }else if(res==214){
                error(res,"当前字段查询已存在");
            }else if(res==215){
                error(res,"表单id为空");
            }else if(res==216){
                error(res,"表单id格式错误");
            }else {
                resultMap.put("result", Const.UPDATE_ERROR_211);
                resultMap.put("msg", Const.UPDATE_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.UPDATE_ERROR_212);
            resultMap.put("msg", Const.UPDATE_FAIL);
        }
        return resultMap;
    }

    /**
     * 删除查询条件
     *
     * @param id 条件id
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteSearchCondition(Long id) {
        try {
            SearchCondition searchCondition=new SearchCondition();
            searchCondition.setId(id);
            int res = service.deleteSearchCondition(searchCondition);
            if (res == 1) {
                resultMap.put("msg", Const.DEL_SUCCEED);
            } else {
                resultMap.put("result", Const.DEL_ERROR_301);
                resultMap.put("msg", Const.DEL_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DEL_ERROR_302);
            resultMap.put("msg", Const.DEL_FAIL);
        }
        return resultMap;
    }

    /**
     * 根据表单id获取查询条件
     *
     * @param formId 表单id
     */
    @RequestMapping(value = "getCondition", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSearchCondition(Long formId) {
        try {
            Map<String, Object> map= service.getSearchCondition(formId);
            if ((int) map.get("res")==1) {
                List<SearchCondition> searchConditionList=(List<SearchCondition>)map.get("data");
                List<SearchConditionOutDto> searchConditionOutDtoList = searchConditionList.stream().map(SearchConditionOutDto::new).collect(Collectors.toList());
                resultMap.put("data",searchConditionOutDtoList);
                resultMap.put("msg", Const.DATA_SUCCEED);
            } else {
                resultMap.put("result", Const.DATA_ERROR_111);
                resultMap.put("msg", Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DATA_ERROR_112);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

    /**
     * 根据表单主键查询字段列表
     * 表单主键复数则查询公有字段列表
     * @param ids
     * @return
     */
    @GetMapping("queryFields")
    @ResponseBody
    private Map<String, Object> queryFields(String ids){
        try {
            resultMap.putAll(service.queryFields(ids));
        }
        catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DATA_ERROR_112);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

}

